﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;
using Xiaowei.Helpers;
using Xiaowei.Settings;

namespace Xiaowei.Models
{
    public class DriverItem
    {
        public string DriverType { get; set; }
        public string DriverName { get; set; }
        public string DriverURL { get; set; }
        public string DriverExe { get; set; }
    }
    public class DriverModel
    {
        public string DriverName { get; set; } = "";
        public LoadingStateEnum LoadingState { get; set; } = LoadingStateEnum.Loading;
        public enum LoadingStateEnum
        {
            Loading,
            Success,
            Fail,
            NoSupport
        }
        public Dictionary<string, List<DriverItem>> DriverList { get; } = new Dictionary<string, List<DriverItem>>();
        public event Action FetchDriverListEnded;
        public void FetchDriverList()
        {
            string pn = "6XL29AV";
            if (!DebugConfig.TestDriverList)
            {
                pn = LocalSettings.ProductNumber;
            }
            string name = DriverName;
            Task.Run(async () =>
            {
                try
                {
                    string data = await ServerApi.Instance.GetDriverList(pn, name).ConfigureAwait(false);
                    JsonObject jo = JsonObject.Parse(data);
                    if (jo["code"].GetNumber() == 200)
                    {
                        JsonArray ja = jo["data"].GetArray();
                        foreach (var item in ja)
                        {
                            JsonObject systemObject = item.GetObject();
                            string systemName = systemObject["osName"].GetString();
                            List<DriverItem> driverItemList = new List<DriverItem>();
                            JsonArray driveList = systemObject["driveList"].GetArray();
                            foreach (var item2 in driveList)
                            {
                                JsonObject driverObject = item2.GetObject();
                                DriverItem driverItem = new DriverItem();
                                driverItem.DriverExe = driverObject.GetNamedString("spName", "").Replace("\r", "").Replace("\n", "");
                                driverItem.DriverURL = driverObject.GetNamedString("link", "").Replace("\r", "").Replace("\n", "");
                                driverItem.DriverType = driverObject.GetNamedString("categoryName", "").Replace("\r", "").Replace("\n", "");
                                driverItem.DriverName = driverObject.GetNamedString("title", "").Replace("\r", "").Replace("\n", "");
                                driverItemList.Add(driverItem);
                            }
                            DriverList.Add(systemName, driverItemList);
                        }
                        LoadingState = LoadingStateEnum.Success;
                    }
                    else if (jo["code"].GetNumber() == 600)
                    {
                        LoadingState = LoadingStateEnum.NoSupport;
                    }
                    else
                    {
                        LoadingState = LoadingStateEnum.Fail;
                    }
                    FetchDriverListEnded?.Invoke();
                    FetchDriverListEnded = null;
                }
                catch
                {
                    LoadingState = LoadingStateEnum.Fail;
                    FetchDriverListEnded?.Invoke();
                    FetchDriverListEnded = null;
                }

            });
        }
    }
}
